package com.woniu.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.dto.ResourceDTO;
import com.woniu.param.InsertPermissionParam;
import com.woniu.server.RedisKey;
import com.woniu.server.RedisUtil;
import com.woniu.user.entity.Permission;
import com.woniu.user.mapper.PermissionMapper;
import com.woniu.user.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RedisUtil redisUtil;
    @Override
    public void addPermission(InsertPermissionParam bean) throws Exception {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("permission_name", bean.getPermissionName());
        Permission permission = this.getOne(queryWrapper);
        if (permission != null) {
            throw new Exception("资源已存在");
        }
        permission= BeanUtil.toBean(bean, Permission.class);
        permissionMapper.insert(permission);
    }


    @Override
    public List<ResourceDTO> selectAdminResource(String adminId) throws Exception {
        String json = redisUtil.getString(RedisKey.adminResource(Integer.parseInt(adminId)));
        List<ResourceDTO> list = JSONUtil.toList(JSONUtil.parseArray(json), ResourceDTO.class);
        return list;
    }

    @Override
    public List<String> selectAdminButton(String adminId, Integer menuId) throws Exception {
        List<String> buttons = permissionMapper.selectButtonByAdminId(adminId, menuId);
        return buttons;
    }

    @Override
    public void selectAdminAll(String adminId) {
        Set<String> list=permissionMapper.selectPermissions(adminId);
        redisUtil.sadd(RedisKey.adminPermission(Integer.parseInt(adminId)), list);
    }

    @Override
    public List<ResourceDTO> selectUserResource(String userId) throws Exception {
        String json = redisUtil.getString(RedisKey.userResource(Integer.parseInt(userId)));
        List<ResourceDTO> list = JSONUtil.toList(JSONUtil.parseArray(json), ResourceDTO.class);
        return list;
    }

    @Override
    public List<String> selectUserButton(String userId, Integer menuId) throws Exception {
        List<String> buttons = permissionMapper.selectButtonByUserId(userId, menuId);
        return buttons;
    }

    @Override
    public void selectUserAll(String userId) throws Exception {
        Set<String> list=permissionMapper.selectPermissionsUser(userId);
        redisUtil.sadd(RedisKey.userPermission(Integer.parseInt(userId)), list);
    }
}
